// SINGULARITY HTML5 GAME ENGINE
// -----------------------------
// Copyright(c) 2013 by Codenova.pl

"use strict";

var SINGULARITY = SINGULARITY || { REVISION: '1' };

// Some preparations
self.console = self.console || {
    info: function () {},
    log: function () {},
    debug: function () {},
    warn: function () {},
    error: function () {}
};

// Trimmer prototype
String.prototype.trim = String.prototype.trim || function () {
    return this.replace( /^\s+|\s+$/g, '' );
};

String.prototype.isEmpty = String.prototype.isEmpty || function(str) {
    return (!str || /^\s*$/.test(str));
};

SINGULARITY.extend = function (obj, source) {
// ECMAScript5 compatibility based on: http://www.nczonline.net/blog/2012/12/11/are-your-mixins-ecmascript-5-compatible/
    var prop;
    if (Object.keys) {
        var keys = Object.keys(source);
        for (var i = 0, il = keys.length; i < il; i++) {
            prop = keys[i];
            Object.defineProperty(obj, prop, Object.getOwnPropertyDescriptor(source, prop));
        }
    } else {
        var safeHasOwnProperty = {}.hasOwnProperty;
        for (prop in source ) {
            if (safeHasOwnProperty.call(source, prop)) {
                obj[prop] = source[prop];
            }
        }
    }
    return obj;
};

// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
// using 'self' instead of 'window' for compatibility with both NodeJS and IE10.
(function () {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];

    for (var x = 0; x < vendors.length && !self.requestAnimationFrame; ++ x) {
        self.requestAnimationFrame = self[vendors[x] + 'RequestAnimationFrame'];
        self.cancelAnimationFrame = self[vendors[x] + 'CancelAnimationFrame']|| self[vendors[x] + 'CancelRequestAnimationFrame'];
    }

    if (self.requestAnimationFrame === undefined && self['setTimeout'] !== undefined ) {
        self.requestAnimationFrame = function(callback) {
            var currTime = Date.now(), timeToCall = Math.max(0, 16 - ( currTime - lastTime));
            var id = self.setTimeout(function() {callback(currTime + timeToCall);}, timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };
    }

    if(self.cancelAnimationFrame === undefined && self['clearTimeout'] !== undefined ) {
        self.cancelAnimationFrame = function (id) {self.clearTimeout(id)};
    }
}());

// RequireJS config
requirejs.config({
    baseUrl: 'src',
    paths: {
        app: './Singularity'
    },
    urlArgs: "ts=" + (new Date()).getTime()
});

// Start the main app logic.
requirejs(['Singularity/Core', 'Singularity/Stage', 'Singularity/Ui/Ui', 'Singularity/Ui/Button', 'Singularity/Ui/RoundedButton', 'Singularity/Ui/ButtonGroup', 'Singularity/Utilities/Particles', 'Singularity/Ui/Form', 'Singularity/Event', 'Singularity/Ui/Textbox', 'Singularity/Modals/Modal'],
    function (Core, Stage, Ui, Button, RoundedButton, ButtonGroup, Particles, Form, Event, Textbox, Modal) {

        // STAGE - Choose fraction
        // =====================================

        // UI elements

        // Modals
        var firstRunModal = Modal({
            Background: 'rgba(255, 255, 255, .2)',
            BorderColor: '#ff0',
            BorderWidth: 1,
            Color: '#fff',
            Height: 100,
            Text: 'Witaj w Singularity! Teraz stworzysz swojego awatara.',
            Width: 'auto',
            X: 'auto',
            Y: 'auto'
        });

        // UIs
        var uiChooseFraction = Ui({
            Width: 'auto',
            Height: 'auto'
        }, []);

        // Stages
        var stageChooseFraction = Stage('chooseFraction', {
            Width: 'auto',
            Height: 'auto'
        }, uiChooseFraction);

        // Stage events
        var stageChooseFractionBeforeRenderEvent = Event('beforeRender', stageChooseFraction, {
            Action: 'modal',
            Data: firstRunModal
        });

        stageChooseFractionBeforeRenderEvent.AttachEvent();

        // STAGE - Main Menu
        // =====================================

        // UI elements
        var buttonLogIn = Button({
            BackgroundColor: 'rgba(0, 0, 0, .9)',
            BorderColor: 'transparent',
            BorderWidth: 0,
            Height: 30,
            Width: 200,
            X: 'auto',
            Y: 100,
            Label: 'Log In',
            LabelColor: '#fff',
            LabelFont: 'Verdana, sans-serif',
            LabelSize: '14pt'
        });

        var textboxLogin = Textbox({
            BackgroundColor: 'rgba(0, 0, 0, .3)',
            BorderColor: 'transparent',
            BorderWidth: 0,
            Height: 30,
            Width: 200,
            X: 'auto',
            Y: 0,
            Label: 'Login',
            LabelColor: '#fff',
            LabelFont: 'Verdana, sans-serif',
            LabelPosition: 'left',
            LabelSize: '12pt'
        });

        var textboxPassword = Textbox({
            BackgroundColor: 'rgba(0, 0, 0, .3)',
            BorderColor: 'transparent',
            BorderWidth: 0,
            Height: 30,
            Width: 200,
            X: 'auto',
            Y: 50,
            Label: 'Password',
            LabelColor: '#fff',
            LabelFont: 'Verdana, sans-serif',
            LabelPosition: 'left',
            LabelSize: '12pt'
        });

        var logInForm = Form({
            Background: 'transparent',
            BorderColor: 'transparent',
            BorderWidth: 0,
            Height: 300,
            Inputs: [textboxLogin, textboxPassword, buttonLogIn],
            Width: 200,
            X: 'auto',
            Y: 'auto'
        });

        // Events
        var buttonLogInClickEvent = Event('click', buttonLogIn, {
            Action: 'stage',
            Data: function() {
                Core.RemoveDomElements('audio');
                Core.RemoveDomElements('input');
                stageChooseFraction.Initialize();
                console.log(stageChooseFraction);
            }
        });

        var buttonLogInHoverEvent = Event('hover', buttonLogIn, {
            BackgroundColor: 'rgba(0, 0, 0, .5)',
            BorderColor: '#fff',
            BorderWidth: 1,
            Height: 30,
            Width: 200,
            X: 'auto',
            Y: 100,
            Audio: 'menu',
            Label: 'Log In',
            LabelColor: '#fff',
            LabelFont: 'Verdana, sans-serif',
            LabelSize: '12pt'
        });

        var textboxLoginHoverEvent = Event('hover', textboxLogin, {
            BackgroundColor: 'rgba(0, 0, 0, .3)',
            BorderColor: '#fff',
            BorderWidth: 1,
            Height: 30,
            Width: 200,
            X: 'auto',
            Y: 0,
            Label: 'Login',
            LabelColor: '#fff',
            LabelFont: 'Verdana, sans-serif',
            LabelSize: '12pt'
        });

        var textboxPasswordHoverEvent = Event('hover', textboxPassword, {
            BackgroundColor: 'rgba(0, 0, 0, .3)',
            BorderColor: '#fff',
            BorderWidth: 1,
            Height: 30,
            Width: 200,
            X: 'auto',
            Y: 50,
            Label: 'Password',
            LabelColor: '#fff',
            LabelFont: 'Verdana, sans-serif',
            LabelSize: '12pt'
        });

        buttonLogInClickEvent.AttachEvent();
        buttonLogInHoverEvent.AttachEvent();
        textboxLoginHoverEvent.AttachEvent();
        textboxPasswordHoverEvent.AttachEvent();

        // UIs
        var uiLogIn = Ui({
            Width: 'auto',
            Height: 'auto'
        }, [logInForm]);

        // Stages
        var stageLogIn = Stage('mainMenu', {
            Width: 'auto',
            Height: 'auto'
        }, uiLogIn);

        stageLogIn.Register(Particles({
            Amount: 50,
            Radius: 1,
            RemovalLimit: 25,
            TTL: 5
        }));
        stageLogIn.Initialize();
});